package micropascalcomp.FSA;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import micropascalcomp.MP_Token;
import micropascalcomp.Token;

/******************************************
 * 
 * GetIdentifier
 * Takes a string as input and outputs a valid comment token
 * @author Jason Welch, Codi Gann, Jamal Martin
 * 
 *******************************************/
public class StringsFSA implements FSABehavior {

    private char quote = 39;

    public MP_Token getToken(FileReader file, LineNumberReader inFile, int colNum) {
        char readChar;
        int col = colNum;
        boolean complete = false;

        try {
            inFile.mark(col);
            readChar = (char) inFile.read();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        if (readChar != quote) {
            return null;
        }

        String lex = "";
        boolean ready = false;
        do {

            lex = lex.concat(Character.toString(readChar));
            col++;

            try {
                //inFile.mark(col);
                readChar = (char) inFile.read();
                
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            if (readChar == quote) {
                lex = lex.concat(Character.toString(readChar));
                col++;
                
                try {
                    //inFile.mark(col);
                    readChar = (char) inFile.read();
                    ready = inFile.ready();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
                if (readChar != quote) {
                    complete = true;
                }
            }
            
            if (readChar == -1) {
                //lex = lex.concat(Character.toString(readChar));
                //col++;

                try {
                    inFile.mark(col);
                    inFile.reset();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new MP_Token(Token.MP_RUN_STRING, lex, inFile.getLineNumber(), colNum);
            
            }
            
        } while (complete != true);

        try {
            inFile.mark(col);
            inFile.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return new MP_Token(Token.valueOf("MP_STRING_LIT"), lex, inFile.getLineNumber(), colNum);
    }
}//end class
